package com.sghd.candy.module.fight.manager;

import com.sghd.common.ramcache.anno.Inject;
import com.sghd.common.ramcache.service.EntityCacheService;
import com.sghd.pojo.candy.fight.enums.Grade;
import org.springframework.stereotype.Component;

/**
 * @author Allen
 * @date 2018/12/20
 */
@Component
public class LevelInfoManager {
    @Inject
    private EntityCacheService<Long, LevelInfo> levelInfoCache;

    public LevelInfo getLevelInfo(long playerId) {
        return levelInfoCache.loadOrCreate(playerId, builder -> LevelInfo.valueOf(playerId));
    }

    public void update(LevelInfo levelInfo, LevelId nextLevelId) {
        levelInfo.update(nextLevelId);
    }

    public int update(LevelInfo levelInfo, LevelId curLevelId, Grade grade) {
        int oldGrade = levelInfo.getLevelGrade().getOrDefault(curLevelId.getBigLevelId() + "-" + curLevelId.getLevelId(), 0);
        int addGrade = 0;
        if (oldGrade < Grade.THREE.ordinal()) {
            addGrade = grade.ordinal() - oldGrade;
            levelInfo.update(curLevelId, grade);
        }
        return addGrade;
    }
}
